Computing square roots using the babylonian method

Computing square roots using the babylonian method.

Perhaps the first algorithm used for approximating vS is known as the
Babylonian method, named after the Babylonians, or “Hero’s method”,
named after the first-century Greek mathematician Hero of Alexandria
who gave the first explicit description of the method.
It can be derived from (but predates by 16 centuries) Newton’s method.
The basic idea is that if x is an overestimate to the square root
of a non-negative real number S then S / x will be an underestimate
and so the average of these two numbers may reasonably be expected
to provide a better approximation.
def babylonian_algorithm(N):

    if N == 0:
        return 0

    g = N / 2.0
    g2 = g + 1
    while g != g2:
        n = N / g
        g2 = g
        g = (g + N)/2

    return g

# test
print('The Square root of 0.3 =', babylonian_algorithm(0.3))

Output:

The Square root of 0.3 = 0.5477225575051661